<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>盘点任务编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
var basic_Url="${ctx}/yd/logic/whcheckmission";
var grid;
$(function(){
	grid = $("#table1").ligerGrid({
        columns: [
				  { display: 'id', name: 'id', width: 100,hide:true},
					  { display: 'zd001TaskID', name: 'zd001TaskID', width: 100,hide:true},
					  { display: 'zd002MaterID', name: 'zd002MaterID', width: 100,hide:true},
	                  { display: '<font color="gray">物料编码</font>', name: 'zd003MaterCode', width: 120},
	                  { display: '<font color="gray">物料名称</font>', name: 'zd004MaterName', width: 120},
	                  { display: '<font color="gray">物料类型</font>', name: 'zd005MaterType', width: 100},
	                  { display: '<font color="gray">规格</font>', name: 'zd006MaterSpec', width: 120},
	                  { display: '<font color="gray">型号</font>', name: 'zd007MaterModel', width: 270},
	                  { display: '<font color="gray">单位</font>', name: 'zd015Unit', width: 80},
	                  { display: '<font color="gray">批次</font>', name: 'zd014Batch', width: 120},
	                  { display: '<font color="gray">总库存数量</font>', name: 'zd016HsQuantity', width: 80,render: function (item){  
							if(item.zd016HsQuantity== null || item.zd016HsQuantity == '' || parseFloat(item.zd016HsQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd016HsQuantity;
	                	 	}
	                  }},
	                  { display: '<font color="gray">总盘点数量</font>', name: 'zd016WhHsQuantity', width: 80,render: function (item){  
							if(item.zd016WhHsQuantity == null || item.zd016WhHsQuantity == '' || parseFloat(item.zd016WhHsQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd016WhHsQuantity;
	                	 	}
	                  }},
					  { display: '<font color="gray">总差异数量</font>', name:'diffQuantity', width: 80,render: function (item){  
							if(item.zd016HsQuantity == null || item.zd016HsQuantity == '' || parseFloat(item.zd016HsQuantity) == 0){
		                		  return '0.0';
		                	} else {
		                		if(item.zd019WhOldTaskQuantity==null||item.zd019WhOldTaskQuantity== ''||parseFloat(item.zd019WhOldTaskQuantity)){
		                			return item.zd016HsQuantity;
		                		}else{
		                			return parseFloat(item.zd016HsQuantity)-parseInt(item.zd019WhOldTaskQuantity);
		                		}
	                	 	}
	                  }},
	                  { display: '<font color="gray">一等品库存数量</font>', name: 'zd017FirstQuantity', width: 100,render: function (item){  
							if(item.zd017FirstQuantity == null || item.zd017FirstQuantity == '' || parseFloat(item.zd017FirstQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd017FirstQuantity;
	                	 	}
	                  }},
	                  { display: '<font color="gray">一等品盘点数量</font>', name: 'zd017WhFirstQuantity', width: 100,render: function (item){  
							if(item.zd017WhFirstQuantity == null || item.zd017WhFirstQuantity == '' || parseFloat(item.zd017WhFirstQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd017WhFirstQuantity;
	                	 	}
	                  }},
					  { display: '<font color="gray">一等品差异数量</font>', name: 'diffFirstQuantity', width: 100,render: function (item){  
							if(item.zd017FirstQuantity == null || item.zd017FirstQuantity == '' || parseFloat(item.zd017FirstQuantity) == 0){
		                		  return '0.0';
		                	} else {
		                		if(item.zd017WhFirstQuantity == null || item.zd017WhFirstQuantity == '' || parseFloat(item.zd017WhFirstQuantity) == 0){
		                			return item.zd017FirstQuantity;
		                		}else{
		                			return parseFloat(item.zd017FirstQuantity)-parseFloat(item.zd017WhFirstQuantity)
		                		}
	                	 	}
	                  }},
	                  { display: '<font color="gray">次品库存数量</font>', name: 'zd018SecondQuantity', width: 100,render: function (item){  
							if(item.zd018SecondQuantity == null || item.zd018SecondQuantity == '' || parseFloat(item.cipinQuantityWH) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd018SecondQuantity;
	                	 	}
	                  }},
	                  { display: '<font color="gray">次品盘点数量</font>', name: 'zd018WhSecondQuantity', width: 100,render: function (item){  
							if(item.zd018WhSecondQuantity == null || item.zd018WhSecondQuantity == '' || parseFloat(item.zd018WhSecondQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd018WhSecondQuantity;
	                	 	}
	                  }},
					  { display: '<font color="gray">次品差异数量</font>', name: 'cipinQuantityCY', width: 100,render: function (item){  
						  if(item.zd018SecondQuantity == null || item.zd018SecondQuantity == '' || parseFloat(item.cipinQuantityWH) == 0){
	                		  return '0.0';
	                		} else {
	                			if(item.zd018WhSecondQuantity == null || item.zd018WhSecondQuantity == '' || parseFloat(item.zd018WhSecondQuantity) == 0){
			                		  return item.zd018SecondQuantity;
			                	} else {
		                	 	 	return parseFloat(item.cipinQuantityWH)-parseFloat(item.zd018WhSecondQuantity);
		                	 	}
	                	 	}
	                  }},
	                  { display: '<font color="gray">旧品库存数量</font>', name: 'zd019OldQuantity', width: 100,render: function (item){  
							if(item.zd019OldQuantity == null || item.zd019OldQuantity == '' || parseFloat(item.zd019OldQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd019OldQuantity;
	                	 	}
	                  }},
	                  { display: '<font color="gray">旧品盘点数量</font>', name: 'zd019WhOldQuantity', width: 100,render: function (item){  
							if(item.zd019WhOldQuantity == null || item.zd019WhOldQuantity == '' || parseFloat(item.zd019WhOldQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd019WhOldQuantity;
	                	 	}
	                  }},
					  { display: '<font color="gray">旧品差异数量</font>', name: 'jiupinQuantityCY', width: 100,render: function (item){  
						  if(item.zd019OldQuantity == null || item.zd019OldQuantity == '' || parseFloat(item.zd019OldQuantity) == 0){
	                		  return '0.0';
	                		} else {
	                			if(item.zd019WhOldQuantity == null || item.zd019WhOldQuantity == '' || parseFloat(item.zd019WhOldQuantity) == 0){
			                		  return item.zd019OldQuantity;
			                	} else {
		                	 	 	return parseFloat(item.zd019OldQuantity) - parseFloat(item.zd019WhOldQuantity);
		                	 	}
	                	 	}
	                  }},
	                  { display: '<font color="gray">冻结库存数量</font>', name: 'zd020FreeQuantity', width: 100,render: function (item){  
							if(item.zd020FreeQuantity == null || item.zd020FreeQuantity == '' || parseFloat(item.zd020FreeQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd020FreeQuantity;
	                	 	}
	                  }},
	                  { display: '<font color="gray">冻结盘点数量</font>', name: 'zd020WhFreeQuantity', width: 100,render: function (item){  
							if(item.zd020WhFreeQuantity == null || item.zd020WhFreeQuantity == '' || parseFloat(item.zd020WhFreeQuantity) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.zd020WhFreeQuantity;
	                	 	}
	                  }},
					  { display: '<font color="gray">冻结差异数量</font>', name: 'freezeQuantityCY', width: 100,render: function (item){  
						  if(item.zd020FreeQuantity == null || item.zd020FreeQuantity == '' || parseFloat(item.zd020FreeQuantity) == 0){
                	 	 		return '0.0';
                	 		} else {
                	 			if(item.zd020WhFreeQuantity == null || item.zd020WhFreeQuantity == '' || parseFloat(item.zd020WhFreeQuantity) == 0){
                	 				item.zd020FreeQuantity;
  		                		}else{
  		                			return parseFloat(item.zd020FreeQuantity)-parseFloat(item.zd020FreeQuantity);
  		                		}
	                	 	}
	                  }}/* ,
	                  { display: '<font color="gray">备注</font>', name: 'remark', width: 220} */
                  ],
                  enabledEdit: true,
                  usePager: true,pageSize:1000,pageSizeOptions:[1000],
                  parms:[{ name : 'id' , value : '${pd.id}'}],
		        	url : basic_Url+"/griditems.do",
                  width: "100%", height:'90%', checkbox: true,
                  toolbar: { items: [
                                     { text: '添加物料', click: ItemWindow, icon: 'add' },
                                     { line: true },
                                     { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                    ]
                            }
             });
	
	/* <c:forEach items="${pditemlist}" var="item">
		grid.addRow2({
			id : "${item.id}",
			checkMissionID : "${item.checkMissionID}",
			materialsID : "${item.materialsID}",
			code: "${item.code}",
			name: "${item.name}",
			maType: "${item.maType}",
			spec: "${item.spec}",
			unit: "${item.unit}",
			batch: "${item.batch}",
			quantityWH : "${item.quantityWH}",
			quantityPD : "${item.quantityPD}",
			quantityCY : "${item.quantityCY}",
			cipinQuantityWH : "${item.cipinQuantityWH}",
			zd018WhSecondQuantity : "${item.zd018WhSecondQuantity}",
			cipinQuantityCY : "${item.cipinQuantityCY}",
			zd019OldQuantity : "${item.zd019OldQuantity}",
			zd019WhOldQuantity : "${item.zd019WhOldQuantity}",
			jiupinQuantityCY : "${item.jiupinQuantityCY}",
			ydengQuantityWH : "${item.ydengQuantityWH}",
			ydengQuantityPD : "${item.ydengQuantityPD}",
			ydengQuantityCY : "${item.ydengQuantityCY}",
			zd020FreeQuantity : "${item.zd020FreeQuantity}",
			zd020WhFreeQuantity : "${item.zd020WhFreeQuantity}",
			freezeQuantityCY : "${item.freezeQuantityCY}",
			remark : "${item.remark}"
		});
	</c:forEach> */
	$("#pageloading").hide();

	$("#inputForm").validate({			   
	onsubmit: true,
	 /*执行ajaxsubmit  */ 
    submitHandler: function(editform) {
        try{			    		
            url =  basic_Url+"/save.do";
            var api = frameElement.api;
        	var W = api.opener;
            getReturnMessage(url,"inputForm",W.searchAP);
        }catch(e){alert(e);}
    }
	});
	if ('${flag}' == 'ADD'){
		$("#save1").hide();//禁用提交按钮
		$("#save2").hide();//禁用申请结束按钮
		$("#save3").hide();//禁用结束任务按钮
		$("#save4").hide();//禁用驳回重盘按钮
		$("#btn_exportExcel").hide();//禁用导出
	}
	if ('${flag}' == 'tijiao'){
		$("#save").hide();//禁用保存按钮
		$("#save2").hide();//禁用申请结束按钮
		$("#save3").hide();//禁用结束任务按钮
		$("#save4").hide();//禁用驳回重盘按钮
		$("#btn_exportExcel").hide();//禁用导出
        if($("#createName").val()!="${user.name}"){
    		$("#save1").hide();//禁用提交按钮
    		$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
    		$("select").attr("disabled","disabled");//禁用SELECT标签
    		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
        } 
	}
	if ('${flag}' == 'weipandian'){
		$("#save").hide();//禁用保存按钮
		$("#save1").hide();//禁用提交按钮
		$("#save2").hide();//禁用申请结束按钮
		$("#save3").hide();//禁用结束任务按钮
		$("#save4").hide();//禁用驳回重盘按钮
		$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}
	if ('${flag}' == 'pandianzhong'){
		$("#save").hide();//禁用保存按钮
		$("#save1").hide();//禁用提交按钮
		if($("#agent").val()!="${user.name}"){
    		$("#save2").hide();//禁用申请结束按钮
        } 
		$("#save3").hide();//禁用结束任务按钮
		$("#save4").hide();//禁用驳回重盘按钮
//		$("#btn_exportExcel").hide();//禁用导出
		$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}
	if ('${flag}' == 'chongpan'){
		$("#save").hide();//禁用保存按钮
		$("#save1").hide();//禁用提交按钮
		if($("#agent").val()!="${user.name}"){
    		$("#save2").hide();//禁用申请结束按钮
        } 
		$("#save3").hide();//禁用结束任务按钮
		$("#save4").hide();//禁用驳回重盘按钮
//		$("#btn_exportExcel").hide();//禁用导出
		$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}
	if ('${flag}' == 'shenhe'){
		$("#save").hide();//禁用保存按钮
		$("#save1").hide();//禁用提交按钮
		$("#save2").hide();//禁用申请结束按钮
		$("#btn_exportExcel").hide();//禁用导出
		$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}
	if ('${flag}' == 'complete'){
		$("#save").hide();//禁用保存按钮
		$("#save1").hide();//禁用提交按钮
		$("#save2").hide();//禁用申请结束按钮
		$("#save3").hide();//禁用结束任务按钮
		$("#save4").hide();//禁用驳回重盘按钮
		$("#btn_exportExcel").hide();//禁用导出
		$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}

    $("#storage").change(function(){ 
		$("#whName").val($("#storage").find("option:selected").text());
		$("#whCode").val($("#storage").val());
		$("#zd001WhID").val($("#storage").find("option:selected").attr("zd001WhID"));
		var manager = $("#table1").ligerGetGridManager(); 
		manager.loadData(); 
	}); 

});

function ItemWindow(){
	if($("#storage").val()==""){
		$.dialog.alert("请选择盘点仓库");
   	 	return false;
	}
	if ('${xx}' == 1){
		getDG().dialog({
			title:'选择物料',
			width : 650,
			height : 350,
			content: 'url:' + sys_context+'/public/windowalert/itemaff.do?code=' + $("#whCode").val(),
			lock : true,
			max : true,
			min : true,
			ok: function(){
				var rows = this.content.getSelected();
				if (rows != null) {
					for (var i = 0; i < rows.length;i++){
						var row = rows[i];
						grid.addRow2({
							zd002MaterID : row.zd003MaterialsId,
							zd003MaterCode : row.zd004MaterialsCode,
							zd004MaterName : row.zd005MaterialsName,
							zd005MaterType : row.maType,
							zd006MaterSpec : row.zd006MaterialsSpec,
							zd015Unit : row.zd008Unit,
							zd014Batch : row.zd020Batch,
							zd016HsQuantity : (row.zd009Quantity==null?0.0:row.zd009Quantity),
							zd016WhHsQuantity : '0',
							zd017FirstQuantity : row.zd019OneQuantity==null?0.0:row.zd019OneQuantity,
							zd017WhFirstQuantity : '0',
							zd018WhSecondQuantity : '0',
							zd019OldQuantity : row.zd016DefectiveQuantity==null?0.0:row.zd016DefectiveQuantity,
							zd019WhOldQuantity : '0',
							zd020FreeQuantity : row.zd010BlockedStockQuantity==null?0.0:row.zd010BlockedStockQuantity,
							zd020WhFreeQuantity : '0',
						});
					}
					 return true;
				}
	    		return false;
		    },
		    cancelVal: '关闭',
		    cancel: true, 
		}).zindex();
	}
}
	
function submitForm(param){
    //设置ajax全局同步
	var data = grid.getData();
 	if(data.length==0){
		$.dialog.alert("请选择物料");
   	 	return false;
	} 
	var ba=true;
	jQuery.ajaxSettings.async = false;
	if(ba){
		$("#billStatus").val(param);
		$("#checkdata").val(JSON.stringify(data));
	    $("#inputForm").submit();
	}
 }	

function tableSubmit(msg,param){
	$.ligerDialog.confirm(msg, 
			function (flag) { 
			if(flag){
				submitForm(param);
				}
		}); 
}

function submitApply(param){
	tableSubmit('是否确认申请结束盘点？',param);
}

function submitEnd(param){
	tableSubmit('是否确认结束盘点任务？',param);
}

function submitRebut(param){
	tableSubmit('是否确认驳回重盘？',param);
}

/* function check(param){
	$.ajax({
	      url: "${ctx}/yd/logic/whcheckmission/updateState.do",
	      global: false,
	      type: "POST",
	      dataType: "json",
	      data: ({
	    	   id:$("#id").val()
	      }),		     
	      success: function(msg){
	    	  if(msg.msg=="SUCCESS"){
	    		  $.dialog.alert("驳回成功",function(){close();});
	    	  }
	    	  if(msg.msg=="ERROR"){
	    		  $.dialog.alert("操作失败",function(){close();});
	    	  }
	   }
	});
} */
function close(){
	var api = frameElement.api;
	var W = api.opener;
	CloseDialog();
	W.searchAP();
}
function deleteRow()
{ 
	if ('${xx}' == 1){
		grid.deleteSelectedRow();
	}
}

function exportExcel(){
//	var data = grid.getData();
//	var itemdata = JSON.stringify(data);
//	var path = basic_Url + "/exportExcel.do?itemdata="+itemdata;
	var checkmissionid = $("#id").val();
	var path = basic_Url + "/exportExcel.do?checkmissionid=" + checkmissionid;
   	window.open(path,"_blank");
}
	
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${pd.id}"/>
<input type="hidden" id="checkdata" name="checkdata" value=""/>
<input type="hidden" id="itemdata" name="itemdata" value=""/>
<input type="hidden" id="version" name="bizVersion" value="${pd.bizVersion}"/>
<input type="hidden" id="billStatus" name="zd003BillStatus" value="${pd.zd003BillStatus}" />
<!-- 用于标识提交按钮 -->
 <div class="pageContent" >
    <div class="panel">
    	<table class="TableBlock" align="center">
    		<c:if test="${!empty pd.zd002TackCode }">
    			<tr >
    				<td nowrap>单据编号:</td>
    				<td >
		        		<input type="text" id="checkMissionCode" name="zd002TackCode" readonly="readonly" class="inp_width_98p" value="${pd.zd002TackCode}" />
		        	</td>
		        	<td nowrap="nowrap" width="20%">建单时间:</td>
						<td width="30%"><input class="inp_width_98p" type="text" id="dctime" name="bizCreatorDate"
							value="<fmt:formatDate value="${empty pd.bizCreatorDate?nowDate:pd.bizCreatorDate}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/></td>
		    	</tr>
   		 	</c:if>
   		 	<tr>
   		 		<td nowrap>仓库:</td>
	 			<td>
	 			<c:choose>
	 			<c:when test="${empty pd.zd001WhName or pd.zd003BillStatus==0}">
              		<select id="storage" name="storage" class="inp_width_98p required">
              			<option></option>
                	<c:forEach items='${wh}' var='state'>
                		<option id="${ state.code}" value='${state.code}' zd001WhID="${state.id}">${state.name}</option>
                	</c:forEach>
             		</select>
             	</c:when>
             	<c:otherwise>
             		<input type="text" id="storage" name="storage" class="inp_width_98p required" readonly="readonly" value="${pd.zd001WhName}" />
             	</c:otherwise>
             	</c:choose>
	                <input type="hidden" id="whName" name="zd001WhName" value="${pd.zd001WhName}"/><!-- 盘点仓库名称 -->
	                <input type="hidden" id="whCode" name="zd001WhCode" value="${pd.zd001WhCode}"/><!-- 盘点仓库代码 -->
	                <input type="hidden" id="zd001WhID" name="zd001WhID" value="${pd.zd001WhID}"/>
	                <script type="text/javascript">
	                	$("#"+'${pd.zd001WhCode}').attr("selected","selected");
	                </script>
             	</td>
             	<td nowrap>制单人:</td>
             	<td>
             		 <input type="hidden" id="agent" name="zd004Agent" value="${pd.zd004Agent}"/>
					<input type="text" id="createName" name="bizCreatorName" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
					<c:if test="${pd.bizCreatorName != null}">
						<script>
							$("#createName").val("${pd.bizCreatorName}");
						</script>
					</c:if>
				</td>
             </tr>
   		     <tr>
   		     	<td nowrap>备注:</td>
		        <td colspan="3"><input id="remark"  type="text" class="inp_width_98p" name="zd005Remark" value="${pd.zd005Remark}" /></td>
		     </tr>
        </table>
        <div id="table1"> </div>        
     </div>
     <div class="formBar">
     	<ul>
     		<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm(0)">保存</button></div></div></li>
		    <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save1" onclick="submitForm(1)">提交</button></div></div></li>
			<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save2" onclick="submitApply(3)">申请结束盘点</button></div></div></li>
		    <webres:displaycontroller elementId="LOGIC_WH_CHECK_MISSION_SH" description="结束盘点任务">
				<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save3" onclick="submitEnd(4)">结束盘点任务</button></div></div></li>
			</webres:displaycontroller>
			<webres:displaycontroller elementId="LOGIC_WH_CHECK_MISSION_BH" description="盘点任务驳回">
			    <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save4" onclick="submitRebut(5)">驳回重盘</button></div></div></li>		    		    	   
			</webres:displaycontroller>
			<li><div class="button"><div class="buttonContent"><button type="button" id="btn_exportExcel" class="export" onclick="exportExcel()">导出</button></div></div> </li>
		    <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	    </ul>
      </div>    
    </div>
</form>
</body>
</html>
